<?php
/**
 * Helper para formataçoes
 *
 * @author Aquiles B da Silva
 */
class Zend_Aquiles_Helpers_Menu extends Zend_View_Helper_Abstract {

    public function menu() {
        return $this;
    }

    public function getMenu() {
        $sessioAcl = new Zend_Session_Namespace('acl');
        $menu = null;
        $html = '';
        
        if (isset($sessioAcl) && isset($sessioAcl->menu)) {
            echo $sessioAcl->menu;
        } else {
            $auth = Zend_Auth::getInstance();
            if ($auth->hasIdentity()) {
                $storageObj = $auth->getStorage()->read();
                $perfil = $storageObj['id']['perfil_acesso'];
                
                $acessos = Application_Model_ModuloPerfilAcesso::getInstance()->getMenu($perfil);

                $pai = '';

                $html .= '<ul id="accordion">';
                
                foreach ($acessos as $acesso) {
                    $html .= '<li>';
                    $html .= '<a href="#" class="btn" id="oh"><div class="imagem"></div><b>'.html_entity_decode(utf8_encode($acesso['nome'])).'</b></a>';
                    $html .= '<ul class="borderwhite">';
                    if ($acesso['consulta'] == 'S') {
                        $menu[$acesso['nome']][] = array('Consulta', '/' . $this->corrigeNome($acesso['chave']) . '/' . 'consulta');
                        $html .='<li><a href="'.'/' . $this->corrigeNome($acesso['chave']) . '/' . 'consulta'.'">'.html_entity_decode(utf8_encode($acesso['nome'])).' - Consulta</a></li>';
                    }
                    if ($acesso['edita'] == 'S') {
                        $menu[$acesso['nome']][] = array('Inserir', '/' . $this->corrigeNome($acesso['chave']) . '/' . 'insere');
                        $html .='<li><a href="'.'/' . $this->corrigeNome($acesso['chave']) . '/' . 'insere'.'">'.html_entity_decode(utf8_encode($acesso['nome'])).' - Inserir</a></li>';
                    }
                    $html .= '</ul>';
                    $html .= '</li>';
                }
                $html .= '</ul>';
            }

           $sessioAcl->menu = $html;
            echo $html;
        }
    }

    private function corrigeNome($str) {
        $words = explode('_', $str);
        

        $return = '';
        $count = 0;
        foreach ($words as $word) {
            if($count > 0){
                $return .= ucfirst(trim($word));
            }else{
                $return .= trim($word);
            }
            $count++;
        }

        if($return == '' || count($words) == 0){
            return $str;
        }
        
        return $return;
    }
}